package com.chat.util;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class ControllerUtils {
	public static String GetRequestParam(HttpServletRequest request, String name, String def){
		   if (request == null) return def;
		   String value = request.getParameter(name);
		   if (value == null || value.equals("")) return def;
		   return value;
		}
	public void echo(HttpServletResponse response, String obj) {
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(obj);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void echo(HttpServletResponse response, JSONObject obj) {
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(obj.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void echoErrParam(HttpServletResponse response, String resCode, String msg) {
		JSONObject obj = new JSONObject();
		obj.put("result", resCode);
		obj.put("msg", msg);
		echo(response, obj);
	}
	
	public void echoErrParam2(HttpServletResponse response, int resCode, String msg) {
		JSONObject obj = new JSONObject();
		obj.put("result", resCode);
		obj.put("msg", msg);
		echo(response, obj);
	}
	
	public void echoOkResult(HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("result", "1");
		echo(response, obj);
	}

	public void echoOkParam(HttpServletResponse response, String msg) {
		JSONObject obj = new JSONObject();
		obj.put("result", "1");
		obj.put("msg", msg);
		echo(response, obj);
	}
}
